home *** CD-ROM | disk | FTP | other *** search
/ World of Education / World of Education.iso / world_x / xcoral16.zip / MARK_REG.C < prev    next >
C/C++ Source or Header  |  1993-01-15  |  6KB  |  252 lines

  1. /*
  2. ** Copyright 1989, 1992 by Lionel Fournigault
  3. **
  4. ** Permission to use, copy, and distribute for non-commercial purposes,
  5. ** is hereby granted without fee, providing that the above copyright
  6. ** notice appear in all copies and that both the copyright notice and this
  7. ** permission notice appear in supporting documentation.
  8. ** The software may be modified for your own purposes, but modified versions
  9. ** may not be distributed.
  10. ** This software is provided "as is" without any expressed or implied warranty.
  11. **
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <X11/Xlib.h>
  17.  
  18. #include "text.h"
  19.  
  20. #define KILLREGION        0
  21. #define COPYREGION    1
  22.  
  23. extern Display *dpy;
  24. extern void StoreInKillBuf ();
  25. static void SmallRegion (), BigRegion ();
  26.  
  27. /*
  28. **    Function name : SetMark
  29. **
  30. **    Description : Marque la position courante.
  31. **
  32. **    Input : Le text courant.
  33. **    Ouput :
  34. */
  35. void SetMark ( text )
  36.     Text *text;
  37. {
  38.     text -> markline = text -> no_current_line;
  39.     text -> markpos = GetNcFromLeft ( text -> buf );
  40.     DisplayMessage ( text -> mwin, "Set Mark" );
  41. }
  42.  
  43.  
  44. /*
  45. **    Function name : ExchangePointMark, 
  46. **
  47. **    Description : Va de la marque au curseur et inversement.
  48. **
  49. **    Input : Le text courant.
  50. **    Ouput :
  51. */
  52. void ExchangePointMark ( text)
  53.     Text *text;
  54. {
  55.     register int pos, cline;
  56.  
  57.     TextCursorOn ( text );
  58.     /* On sauve la position courante */
  59.     pos = GetNcFromLeft ( text -> buf );
  60. #ifdef DEBUG
  61.     (void) fprintf ( stderr, "current = %d mark = %d\n",
  62.         pos, text -> markpos );
  63. #endif DEBUG
  64.     cline = text -> no_current_line;
  65.        if ( (cline == text -> markline) && (pos == text -> markpos) ) 
  66.            return;
  67.    
  68.     if ( text -> markline != cline ) {
  69.         GotoLineNumber ( text, text -> markline );
  70.         if ( text -> markpos != 0 )
  71.             (void) MoveHole ( text -> buf, text -> markpos );
  72.         CurrentLineToMiddle ( text );
  73.         SetAndDisplayPage ( text );
  74.         if ( text -> no_current_line != 1 )
  75.             (void) MoveScrollBar ( dpy, text -> swin, CURRENT,
  76.                 text -> no_current_line - text -> n1 - 1 );
  77.         else
  78.             (void) MoveScrollBar ( dpy, text -> swin, FIRST, 0 );
  79.     }
  80.        else {
  81.         (void) MoveHole ( text -> buf,  text -> markpos - pos );
  82.         SetCurrentLine ( text );
  83.         }
  84.     text -> markline = cline;
  85.     text -> markpos = pos;
  86.     TextCursorOn ( text );
  87. }
  88.  
  89.  
  90. /*
  91. **    Function name : KillRegion
  92. **
  93. **    Description : Efface le texte, de la marque au cursor.
  94. **
  95. **    Input :  Le text courant.
  96. **    Ouput :
  97. */
  98. void KillRegion ( text )
  99.     Text *text;
  100. {
  101.     if ( text -> markline == 0 ) {
  102.         DisplayMessage ( text -> mwin, "No mark" );
  103.         return;
  104.     }
  105.        if ( text -> no_current_line == text -> markline ) 
  106.         (void) SmallRegion ( text, KILLREGION );
  107.        else 
  108.         (void) BigRegion ( text, KILLREGION );
  109.     TextCursorOn ( text );
  110. }
  111.  
  112. /*
  113. **    Function name : CopyRegion
  114. **
  115. **    Description : Copie de la marque au curseur.
  116. **
  117. **    Input : Le text courant.
  118. **    Ouput :
  119. */
  120. void CopyRegion ( text )
  121. Text *text;
  122. {
  123.     if ( text -> markline == 0 ) {
  124.         DisplayMessage ( text -> mwin, "No mark" );
  125.         return;
  126.     }
  127.  
  128.        if ( text -> no_current_line == text -> markline ) 
  129.         (void) SmallRegion ( text, COPYREGION );
  130.        else 
  131.         (void) BigRegion ( text, COPYREGION );
  132.     TextCursorOn ( text );
  133. }
  134.  
  135. /*
  136. **    Function name : SmallRegion
  137. **
  138. **    Description : Copie ou efface une region contenue
  139. **        dans la ligne courante.
  140. **
  141. **    Input : Le text courant, l'operation a effectuer.
  142. **    Ouput :
  143. */
  144. static void SmallRegion ( text, op )
  145.     Text *text;
  146.     register int op;
  147. {
  148.     register int diff;
  149.     register int c_pos;
  150.     char buf [32];
  151.  
  152.     /*
  153.      * La marque et le curseur sont sur la meme ligne
  154.      */
  155.     TextCursorOff ( text );
  156.     c_pos = GetNcFromLeft ( text -> buf );
  157.     if ( (diff = text -> markpos - c_pos) == 0 ) {
  158.         TextCursorOn ( text );
  159.         return;
  160.     }
  161.     if ( diff > 0 ) {
  162.         /* curseur avant la marque */
  163.         StoreInKillBuf ( RightBuf ( text -> buf ) , diff, 1 ); 
  164.         if ( op = KILLREGION ) {
  165.             (void) MoveHole ( text -> buf, diff );
  166.             DeleteNchar ( text -> buf, diff );
  167.         }
  168.         else {
  169.             (void) sprintf ( buf, "%d bytes copied", diff );
  170.             DisplayMessage ( text -> mwin, buf );
  171.         }
  172.     }
  173.     else if ( diff < 0 ) {
  174.         /* curseur apres la marque */
  175.         StoreInKillBuf ( LeftBuf ( text -> buf ) + diff + 1, - diff, 1 ); 
  176.         if ( op == KILLREGION ) {
  177.             DeleteNchar ( text -> buf, -diff );
  178.         }
  179.         else {
  180.             (void) sprintf ( buf, "%d bytes copied", -diff );
  181.             DisplayMessage ( text -> mwin, buf );
  182.         }
  183.     }
  184.     SetCurrentLine ( text );
  185.     if ( op == KILLREGION )
  186.         SetTextModif ( text);
  187. }
  188.  
  189.  
  190. /*
  191. **    Function name : BigRegion
  192. **
  193. **    Description : Copie ou efface une region de plusieurs lignes.
  194. **
  195. **    Input : Le text courant, l'operation a effectuer.
  196. **    Ouput :
  197. */
  198. static void BigRegion ( text, op )
  199.     Text *text;
  200.     register int op;
  201. {
  202.     register int diff, lines;
  203.     int len;
  204.     char buf [32];
  205.  
  206.     if ( (lines = text -> no_current_line - text -> markline) > 0 ) 
  207.               (void) GetBackwardLine ( text -> buf, -lines, &len );
  208.     else
  209.         (void) GetForwardLine ( text -> buf,  -lines, &len );
  210.                  
  211.     if ( (diff = len + text -> markpos) < 0 )
  212.         diff *= -1;
  213.       
  214.     if ( lines > 0 )
  215.                    StoreInKillBuf ( LeftBuf ( text -> buf ) - diff + 1, diff, lines );
  216.     else {
  217.         StoreInKillBuf ( RightBuf ( text -> buf ) , diff, -lines );
  218.         if ( op == KILLREGION )
  219.             (void) MoveHole ( text -> buf, diff );
  220.     }
  221.          if ( op == KILLREGION ) {
  222.         DeleteNchar ( text -> buf, diff );
  223.         if ( lines > 0 )
  224.               text -> no_current_line -= lines;
  225.         text -> lines_in_buf = GetNumberOfLineInBuf ( text -> buf ); 
  226.         SetScrollLine ( text -> swin , text -> lines_in_buf );
  227.         SetScrollBarSize ( dpy, text -> swin );
  228.         CurrentLineToMiddle ( text ); 
  229.     }
  230.     else {
  231.         (void) sprintf ( buf, "%d bytes copied", diff );
  232.         DisplayMessage ( text -> mwin, buf );
  233.     }
  234.     TextCursorOff ( text );
  235.     if ( op == KILLREGION )
  236.         SetTextModif ( text);
  237. }
  238.  
  239.  
  240. /*
  241. **    Function name : PasteRegion
  242. **
  243. **    Description : 
  244. **    Input : 
  245. **    Ouput :
  246. */
  247. void PasteRegion ( text )
  248.     Text *text;
  249. {
  250.     Control_Y ( text, 0 );    
  251. }
  252.